﻿using SqlSugar;

namespace NetCore.Repository.suger
{
    /***这里面写的代码不会给覆盖,如果要重新生成请删除 Repository.cs ***/

    /// <summary>
    /// 仓储模式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DbContext<T> where T : class, new()
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context"></param>
        public DbContext()//注意这里要有默认值等于null
        {
            /// <summary>
            /// SqlSugarScope操作数据库是线程安的可以单例
            /// </summary>
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                DbType = DbType.MySql,
                ConnectionString = BaseDBConfig.Main_ConnectionString,//主库数据库连接
                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                IsAutoCloseConnection = true,
                //从库数据库连接
                //SlaveConnectionConfigs = new List<SlaveConnectionConfig>()
                //{
                //    new SlaveConnectionConfig()
                //    {
                //        HitRate = 10,//HitRate 越大走这个从库的概率越大
                //        ConnectionString = BaseDBConfig.From_ConnectionString[0]//从库数据库连接
                //    }
                //}
            });

            //如果用单例配置要统一写在这儿
            //Db.Aop.OnLogExecuting = (sql, pars) =>
            //{
            //    Console.WriteLine(sql + "\r\n" +
            //    Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            //    Console.WriteLine();
            //};

            //Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff") + "开始查询");
            ////List<OrderDrawingequipment> lst = new List<OrderDrawingequipment>();
            ////lst = Db.Queryable<OrderDrawingequipment>().ToList();
            //List<OrderLayerwindingmachineequipment> lst = new List<OrderLayerwindingmachineequipment>();
            //lst = Db.Queryable<OrderLayerwindingmachineequipment>().ToList();
        }

        //注意：不能写成静态的
        public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作

        public SimpleClient<T> CurrentDb
        { get { return new SimpleClient<T>(Db); } }//用来操作当前表的数据
    }
}